﻿using System.Net.Http.Headers;
using Newtonsoft.Json;
using SE.PO.CInterface.DBProtocols.Devices;
using SE.PO.CInterface.Enums;
using SE.PO.CInterface.Po.Equips;

namespace SE.PO.CInterface.Web.Clients;

public class DeviceClient : CurdClientBase<string, DeviceDto, CreateUpdateDeviceDto>
{
    public override string GetRoute() => "devices";

    public DeviceClient(IHttpClientFactory clientFactory, IConfiguration configuration) : base(clientFactory,configuration)
    {
    }

    public async Task<PageData<EquipmentDto>?> GetEquips(string equipsName)
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{BaseUrl}/api/app/devices/equips?equipsName={equipsName}"),
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var items = JsonConvert.DeserializeObject<EquipmentDto[]>(body);
        if (items != null)
        {
            var pageData = new PageData<EquipmentDto>
            {
                TotalCount = items.Length,
                Items = items
            };
            Console.WriteLine(body);
            return pageData;
        }

        return default;
    }

    public async Task<DeviceSummary[]?> GetDevicesSummary()
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{BaseUrl}/api/app/devices/device-summary"),
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var items = JsonConvert.DeserializeObject<DeviceSummary[]>(body);
        return items;
    }
    
    public async Task<DeviceDto[]?> GetDevicesByDeviceType(DeviceTypeEnum _deviceType)
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{BaseUrl}/api/app/devices/by-device-type?deviceType={_deviceType}"),
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var items = JsonConvert.DeserializeObject<DeviceDto[]>(body);
        return items;
    }
    
    public async Task<DeviceDto[]?> GetDevicesByDeviceType(string deviceType)
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Get,
            RequestUri = new Uri($"{BaseUrl}/api/app/devices/by-device-type-str?deviceType={deviceType}"),
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var items = JsonConvert.DeserializeObject<DeviceDto[]>(body);
        return items;
    }
    
    public async Task<bool> SyncDevicess(IEnumerable<CreateUpdateDeviceDto> deviceDtos)
    {
        var client = ClientFactory.CreateClient();
        var request = new HttpRequestMessage
        {
            Method = HttpMethod.Post,
            RequestUri = new Uri($"{BaseUrl}/api/app/devices/sync-devices"),
            Content = new StringContent(JsonConvert.SerializeObject(deviceDtos))
            {
                Headers =
                {
                    ContentType = new MediaTypeHeaderValue("application/json")
                }
            }
        };
        using var response = await client.SendAsync(request);
        response.EnsureSuccessStatusCode();
        var body = await response.Content.ReadAsStringAsync();
        var ok = JsonConvert.DeserializeObject<bool>(body);
        return ok;
    }
}